﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    List<vattu> lst;
    vattu vt;
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader dr;
    String Hoa_don_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.GetPostBackEventReference(this, String.Empty);
        gv.RowDeleting += new GridViewDeleteEventHandler(gv_RowDeleting);

        Hoa_don_id = Request.QueryString["Hoa_don_id"];


        if(IsPostBack==false){   
     
            if (Hoa_don_id == null)
                Hoa_don_id = "0";

            if (Hoa_don_id == "0")
            {
                lbtrangthai.Text = "Thêm mới Hóa Đơn";
                makhach.Focus();
            }

            else
            {
                lbtrangthai.Text = "cập nhật Hóa Đơn " + Hoa_don_id;
                checkall check = new checkall();

                String falg = check.checkDataString("2", Hoa_don_id);
                lbloi.Text = falg;
                if (falg == "")
                {
                    //lay du lieu vao gridview
                    datasever data = new datasever();
                    conn = data.getconnect();
                    cmd = new SqlCommand("sp_tbl_chi_tiet_hoa_don_select", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Hoa_don_id", Hoa_don_id));
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "tbl_chi_tiet_hoa_don");
                    gv.DataSource = ds.Tables["tbl_chi_tiet_hoa_don"];
                    gv.DataBind();

                    //lay du lieu vao textbox hoa don
                    cmd = new SqlCommand("sp_tbl_hoa_don_select", conn);
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Hoa_don_id", Hoa_don_id));
                    dr = cmd.ExecuteReader();
                    dr.Read();
                    String manv = dr["Ma_nhan_vien_sua"].ToString();
                    if (dr["Ma_nhan_vien_sua"].ToString() == "")
                    {
                        manv = dr["Ma_nhan_vien_tao"].ToString();
                    }
                    manhanvien.Text = manv;
                    makhach.Text = dr["Ma_khach"].ToString();
                    sotienhd.Text = dr["So_tien"].ToString();
                    soluonghd.Text = dr["So_luong"].ToString();
                    conn.Close();
                }
            }
        }


        String b = Request.Form["__EVENTTARGET"];
        if (Request.Form["__EVENTTARGET"] == "mavattu")
        {
            if (mavattu.Text == "")
            {
                thanhtoan_Click1(null, null);
            }
        }
    }

    void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //xoa vat tu
        vattu vt = new vattu();
        vt.delete(gv, e.RowIndex);
        List<vattu> lst1 = new List<vattu>();
    }

    //tao inputindex cho textbox trong gridview
    int i = 0;
    public int index()
    {
        i++;
        return i;
    }
   
    protected void thanhtoan_Click1(object sender, EventArgs e)
    {

        checkall check = new checkall();
        List<WebControl> lst = new List<WebControl>();
        lst.Add(makhach);
        lst.Add(manhanvien);
        String falg = check.checkdata(lst,"");
        lbloi.Text = falg;

        if (falg == "")
        {

            //duyet textbox so luong trong gridvew
            lst = new List<WebControl>();
            foreach (GridViewRow row in gv.Rows)
            {
                TextBox t = ((TextBox)row.FindControl("txtsoluong"));
                lst.Add(t);
            }
            lst.Add(soluonghd);
            lst.Add(sotienhd);

            //loi nhap so so luong trong gridview
            String l = check.checkint01(lst);
            lbloi.Text = l;
            if (l == "")
            {
                if (gv.Rows.Count == 0)
                {
                    lbloi.Text = "bạn chưa nhập vật tư  ";
                }
                else
                {
                    String sqlHoaDon = "<Hoadon mahoadon=\"" + Hoa_don_id + "\" makhach=\"" + makhach.Text + "\" sotien=\"" + sotienhd.Text + "\" soluong=\"" + soluonghd.Text + "\" Ma_nhan_vien=\"" + manhanvien.Text + "\">";
                    String sqlVatTu = "";

                    // lay chuoi xml vat tu
                    foreach (GridViewRow row in gv.Rows)
                    {
                        TextBox t = ((TextBox)row.FindControl("txtsoluong"));
                        TextBox t1 = ((TextBox)row.FindControl("txtsotien"));
                        sqlVatTu = sqlVatTu + "<Vattu mavt=\"" + row.Cells[0].Text + "\" soluong=\"" + t.Text + "\" dongia=\"" + row.Cells[2].Text + "\" tien=\"" + t1.Text + "\"></Vattu>";
                    }

                    sqlHoaDon = sqlHoaDon + "<Chitiet>" + sqlVatTu + "</Chitiet>" + "</Hoadon>";

                    datasever data = new datasever();
                    conn = data.getconnect();
                    conn.Open();
                    cmd = new SqlCommand("sp_tbl_hoa_don_insert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@doc", sqlHoaDon));
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    lbloi.Text = "thanh cong";

                    //reset lai trang
                    gv.DataSource = null;
                    gv.DataBind();
                    makhach.Text = "";
                    manhanvien.Text = "";
                    soluonghd.Text = "";
                    sotienhd.Text = "";
                    mavattu.Text = "";
                }
            }
        }
    }

    protected void mavattu_TextChanged(object sender, EventArgs e)
    {
        
        List<WebControl> lst = new List<WebControl>();
      

        
        checkall check = new checkall();
       
            lst = new List<WebControl>();
            lst.Add(mavattu);
            String flag = check.checkdata(lst,"1");
            lbloi.Text = flag;

            // flag=="" vat tu ton tai trong co so du lieu
            if (flag == "")
            {
                vattu gvclass = new vattu();
                gvclass.add(gv, "1", mavattu.Text.ToString());
                mavattu.Text = "";
               // soluong.Text = "";
                mavattu.Focus();
            }
        
    }
}